<template>
  <el-select v-model="elModel" filterable :clearable="clearable" :disabled="disabled" :placeholder="placeholder" @change="handleChange">
    <el-option
      v-for="(item, index) in options"
      :key="index"
      :label="item.value"
      :value="item.code + ''"
    />
  </el-select>
</template>

<script>

export default {
  name: 'HeySelect',
  model: {
    prop: 'model',
    event: 'set-model'
  },
  props: {
    model: {
      required: true,
      type: [String, Array, Object]
    },
    clcd: {
      required: true,
      type: String
    },
    placeholder: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    clearable: {
      type: Boolean,
      default: true
    },
    exCludeKeys: {
      type: Array,
      default: () => {
        return []
      }
    },
    inCludeKeys: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      elModel: '',
      options: {}
    }
  },
  computed: {
  },
  watch: {
    model(newVal) {
      this.elModel = newVal
    }
  },
  async mounted() {
    this.elModel = this.model
    await this.getOptions(this.clcd).then(ret => {
      if (ret) {
        const { exCludeKeys,inCludeKeys } = this
        if(inCludeKeys.length>0 && exCludeKeys.length>0){
          this.options = []
        }else if(inCludeKeys.length>0){
          this.options = ret.filter(item => inCludeKeys.includes(item.code))
        }else if(exCludeKeys.length>0){
          this.options = ret.filter(item => !exCludeKeys.includes(item.code))
        }else{
          this.options = ret
        }
      }
    })
  },
  methods: {
    handleChange(val) {
      this.$emit('set-model', val)
      this.$emit('change', val)
    }
  }
}
</script>

<style scoped>
</style>
